package com.jeeplus.modules.sys.voteaccountonstate.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.voteaccountonstate.entity.VoteAccountOnstate;
import com.jeeplus.modules.sys.voteaccountonstate.service.VoteAccountOnstateService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;


@RestController
@RequestMapping(value = "/votedata/voteonstate/voteAccountOnstate")
public class VoteAccountOnstateController extends BaseController {

    @Autowired
    private VoteAccountOnstateService voteAccountOnstateService;

    @ModelAttribute
    public VoteAccountOnstate get(@RequestParam(required=false) String id) {
        VoteAccountOnstate entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = voteAccountOnstateService.get(id);
        }
        if (entity == null){
            entity = new VoteAccountOnstate();
        }
        return entity;
    }

    /**
     * 记录用户登录ip、次数列表数据
     */
    @RequiresPermissions("votedata:voteonstate:voteAccountOnstate:list")
    @GetMapping("list")
    public AjaxJson list(VoteAccountOnstate voteAccountOnstate, HttpServletRequest request, HttpServletResponse response) {
        Page<VoteAccountOnstate> page = voteAccountOnstateService.findPage(new Page<VoteAccountOnstate>(request, response), voteAccountOnstate);
        return AjaxJson.success().put("page",page);
    }

    /**
     * 根据Id获取记录用户登录ip、次数数据
     */
    @RequiresPermissions(value={"votedata:voteonstate:voteAccountOnstate:view","votedata:voteonstate:voteAccountOnstate:add","votedata:voteonstate:voteAccountOnstate:edit"},logical= Logical.OR)
    @GetMapping("queryById")
    public AjaxJson queryById(VoteAccountOnstate voteAccountOnstate) {
        return AjaxJson.success().put("voteAccountOnstate", voteAccountOnstate);
    }

    /**
     * 保存记录用户登录ip、次数
     */
    @RequiresPermissions(value={"votedata:voteonstate:voteAccountOnstate:add","votedata:voteonstate:voteAccountOnstate:edit"},logical=Logical.OR)
    @PostMapping("save")
    public AjaxJson save(VoteAccountOnstate voteAccountOnstate, Model model) throws Exception{
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(voteAccountOnstate);
        if (StringUtils.isNotBlank(errMsg)){
            return AjaxJson.error(errMsg);
        }
        //新增或编辑表单保存
        voteAccountOnstateService.save(voteAccountOnstate);//保存
        return AjaxJson.success("保存记录用户登录ip、次数成功");
    }


    /**
     * 批量删除记录用户登录ip、次数
     */
    @RequiresPermissions("votedata:voteonstate:voteAccountOnstate:del")
    @DeleteMapping("delete")
    public AjaxJson delete(String ids) {
        String idArray[] =ids.split(",");
        for(String id : idArray){
            voteAccountOnstateService.delete(new VoteAccountOnstate(id));
        }
        return AjaxJson.success("删除记录用户登录ip、次数成功");
    }

    /**
     * 导出excel文件
     */
    @RequiresPermissions("votedata:voteonstate:voteAccountOnstate:export")
    @GetMapping("export")
    public AjaxJson exportFile(VoteAccountOnstate voteAccountOnstate, HttpServletRequest request, HttpServletResponse response) {
        try {
            String fileName = "记录用户登录ip、次数"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<VoteAccountOnstate> page = voteAccountOnstateService.findPage(new Page<VoteAccountOnstate>(request, response, -1), voteAccountOnstate);
            new ExportExcel("记录用户登录ip、次数", VoteAccountOnstate.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            return AjaxJson.error("导出记录用户登录ip、次数记录失败！失败信息："+e.getMessage());
        }
    }

    /**
     * 导入Excel数据

     */
    @RequiresPermissions("votedata:voteonstate:voteAccountOnstate:import")
    @PostMapping("import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<VoteAccountOnstate> list = ei.getDataList(VoteAccountOnstate.class);
            for (VoteAccountOnstate voteAccountOnstate : list){
                try{
                    voteAccountOnstateService.save(voteAccountOnstate);
                    successNum++;
                }catch(ConstraintViolationException ex){
                    failureNum++;
                }catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum>0){
                failureMsg.insert(0, "，失败 "+failureNum+" 条记录用户登录ip、次数记录。");
            }
            return AjaxJson.success( "已成功导入 "+successNum+" 条记录用户登录ip、次数记录"+failureMsg);
        } catch (Exception e) {
            return AjaxJson.error("导入记录用户登录ip、次数失败！失败信息："+e.getMessage());
        }
    }

    /**
     * 下载导入记录用户登录ip、次数数据模板
     */
    @RequiresPermissions("votedata:voteonstate:voteAccountOnstate:import")
    @GetMapping("import/template")
    public AjaxJson importFileTemplate(HttpServletResponse response) {
        try {
            String fileName = "记录用户登录ip、次数数据导入模板.xlsx";
            List<VoteAccountOnstate> list = Lists.newArrayList();
            new ExportExcel("记录用户登录ip、次数数据", VoteAccountOnstate.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            return AjaxJson.error( "导入模板下载失败！失败信息："+e.getMessage());
        }
    }


}